package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.ZjAdmOrderTableField;
import com.zb.entity.ZjWorkTableField;
import com.zb.mapper.ZjWorkTableFieldMapper;
import com.zb.service.ZjWorkTableFieldService;
import com.zb.vo.OrderTableInitFieldVo;
import com.zb.vo.WorkTableFieldListVo;
import com.zb.vo.WorkTableFieldVo;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工单列表表头字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-24
 */
@Service
public class ZjWorkTableFieldServiceImpl extends ServiceImpl<ZjWorkTableFieldMapper, ZjWorkTableField> implements ZjWorkTableFieldService {
    @Resource
    private ZjWorkTableFieldMapper workTableFieldMapper;


    // 保存或更新工单表头字段
    @Override
    @Transactional
    public void save(AccounLoginInfo loginInfo, Integer type, List<WorkTableFieldVo> tableFieldVos) {
        Assert.notEmpty(tableFieldVos, "表头字段不能为空，至少一条数据");
        for (WorkTableFieldVo vo : tableFieldVos) {
            Assert.notNull(vo.getAdmOrderTableFieldId(), "原始表头字段ID不能为空");
            Assert.notNull(vo.getName(), "字段名称不能为空");
            Assert.notNull(vo.getShowColumn(), "显示列选项不能为空");
            Assert.notNull(vo.getFreezeColumn(), "冻结列选项不能为空");
            Assert.notNull(vo.getSort(), "排序项不能为空");
            Assert.isTrue(vo.getName().length() >= 2 && vo.getName().length() <= 10, "表头字段名称长度限制输入 2 ~ 10位");
            Assert.isTrue(vo.getSort().toString().length() <= Integer.MAX_VALUE, "排序数据值不合法，数据超长");
        }
        List<ZjAdmOrderTableField> admOrderTableFields = workTableFieldMapper.getAllInitOrderTableFieldList();
        List<String> admOrderTableFieldIds = tableFieldVos.stream().map(vo -> {
                    return vo.getAdmOrderTableFieldId().toString();
                })
                .filter(f -> admOrderTableFields.stream().noneMatch(f2 -> Objects.equals(f2.getId().toString(), f))).toList();
        Assert.isTrue(CollectionUtil.isEmpty(admOrderTableFieldIds), "原始表头字段ID参数不存在系统中：" + String.join("、", admOrderTableFieldIds));

        List<ZjWorkTableField> tableFieldList = workTableFieldMapper.getOrderTableFieldList(loginInfo.getCustAccId(), type, loginInfo.getId());

        List<ZjWorkTableField> adds = new ArrayList<>();
        List<ZjWorkTableField> updates = new ArrayList<>();
        ZjWorkTableField tableField = null;
        Long id = null;
        for (WorkTableFieldVo vo : tableFieldVos) {
            tableField = new ZjWorkTableField();
            BeanUtil.copyProperties(vo, tableField);
            if (null != vo.getId()) {
                updates.add(tableField);
                continue;
            }
            id = tableFieldList.stream().filter(f -> Objects.equals(f.getAdmOrderTableFieldId(), vo.getAdmOrderTableFieldId())).map(ZjWorkTableField::getId).findFirst().orElse(null);
            if (null != id) {
                tableField.setId(id);
                updates.add(tableField);
                continue;
            }
            tableField.setId(IdWorker.getId());
            tableField.setCustAccId(loginInfo.getCustAccId());
            tableField.setAccountId(loginInfo.getId());
            tableField.setType(type);
            adds.add(tableField);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 获取工单表头初始化字段
    @Override
    public List<OrderTableInitFieldVo> getOrderTableInitField() {
        List<ZjAdmOrderTableField> admOrderTableFields = workTableFieldMapper.getInitOrderTableFieldList();
        return admOrderTableFields.stream().map(field -> {
            OrderTableInitFieldVo vo = new OrderTableInitFieldVo();
            BeanUtil.copyProperties(field, vo);
            return vo;
        }).toList();
    }

    // 获取工单表头字段设置项
    @Override
    public List<WorkTableFieldListVo> getOrderTableField(AccounLoginInfo loginInfo, Integer type) {
        List<ZjWorkTableField> tableFieldList = workTableFieldMapper.getOrderTableFieldList(loginInfo.getCustAccId(), type, loginInfo.getId());
        List<ZjAdmOrderTableField> admOrderTableFields = workTableFieldMapper.getInitOrderTableFieldList();
        boolean isTableField = tableFieldList.size() > 0;
        List<WorkTableFieldListVo> voList = admOrderTableFields.stream().map(admField -> {
            WorkTableFieldListVo vo = new WorkTableFieldListVo();
            BeanUtil.copyProperties(admField, vo);
            vo.setAdmOrderTableFieldId(admField.getId());
            vo.setCode(null);
            vo.setInitFieldName(admField.getName());
            if (isTableField) {
                ZjWorkTableField tableField = tableFieldList.stream().filter(f -> Objects.equals(f.getAdmOrderTableFieldId(), admField.getId())).findFirst().orElse(null);
                if (null != tableField) {
                    vo.setId(tableField.getId());
                    vo.setName(tableField.getName());
                    vo.setShowColumn(tableField.getShowColumn());
                    vo.setFreezeColumn(tableField.getFreezeColumn());
                    vo.setSort(tableField.getSort());
                } else {
                    vo.setId(null);
                    vo.setShowColumn(false);
                    vo.setFreezeColumn(false);
                }
            } else {
                vo.setId(null);
            }
            return vo;
        }).toList();
        return voList.stream().sorted(Comparator.comparing(WorkTableFieldListVo::getSort)).toList();
    }

    // 获取工单列表表头字段
    @Override
    public List<WorkTableFieldListVo> getOrderListTableField(AccounLoginInfo loginInfo, Integer type) {
        List<ZjWorkTableField> tableFieldList = workTableFieldMapper.getOrderTableFieldList(loginInfo.getCustAccId(), type, loginInfo.getId());
        List<ZjAdmOrderTableField> admOrderTableFields = workTableFieldMapper.getInitOrderTableFieldList();
        if (CollectionUtil.isEmpty(tableFieldList)) {
            return admOrderTableFields.stream().filter(f -> !f.getDisabled()).map(admField -> {
                WorkTableFieldListVo vo = new WorkTableFieldListVo();
                BeanUtil.copyProperties(admField, vo);
                vo.setId(null);
                return vo;
            }).toList();
        }
        List<WorkTableFieldListVo> voList = new ArrayList<>();
        ZjAdmOrderTableField admOrderTableField = null;
        WorkTableFieldListVo vo = null;
        for (ZjWorkTableField tableField : tableFieldList) {
            admOrderTableField = admOrderTableFields.stream().filter(f -> Objects.equals(f.getId(), tableField.getAdmOrderTableFieldId())).findFirst().orElse(null);
            if (null != admOrderTableField) {
                vo = new WorkTableFieldListVo();
                BeanUtil.copyProperties(tableField, vo);
                vo.setAdmOrderTableFieldId(null);
                vo.setCode(admOrderTableField.getCode());
                voList.add(vo);
            }
        }
        return voList;
    }
}
